package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Habitus;
import com.itheima.service.HabitusService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/habitus")
@RestController
public class HabitusController {
    @Reference
    private HabitusService habitusService;

    @RequestMapping("/findPage")
    private PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       return habitusService.findPage(queryPageBean);
    }

    @RequestMapping("/findFormData")
    public Result findFormData(){
        try {
            List<Habitus> habituses = habitusService.findFormData();
            return  new Result(true, MessageConstant.GET_HAB_SUCCESS,habituses);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_HAB_FALL);
        }
    }

    /**
     * 提交测评表单
     * @param habituses
     * @param id member id
     * @return
     */
    @RequestMapping("/addReport")
    public Result addReport(@RequestBody List<Habitus> habituses,Integer id){
        System.out.println(id);
        try {
            habitusService.addReport(habituses,id);
            return new Result(true,"提交测评成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"提交测评失败，请检查是否填写完整");
        }

    }

    @RequestMapping("/deleteReport")
    public Result deleteReport(Integer id){
        try {
            habitusService.deleteReport(id);
            return new Result(true,"删除测评成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除测评失败");
        }
    }

    @RequestMapping("/findOneById")
    public Result findOneById (Integer id){
        try {
           Map<String,Object> report = habitusService.findOneById(id);
            return new Result(true,"获取体质数据成功",report);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"获取体质数据失败");
        }
    }

    @RequestMapping("/updateHabitusStatus")
    public Result updateHabitusStatus(@RequestBody List<Habitus> habituses,int id){
        try {
            habitusService.updateHabitusStatus(habituses,id);
            return new Result(true,"修改体质成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改体质失败");
        }

    }



}

